<?php

namespace V1\Model;


use Common\Common\Tools;
use Think\Model;

class QuestionAskModel extends Model
{
    protected $tableName = 'question_ask';

    /**
     * 通过问题ID获取提问列表
     * @param $askid
     * @return mixed
     */
    public function get_id_by_ask_list($askid)
    {
        $area = new AreaModel();
        $data = $this->alias('a')
            ->join('LEFT JOIN destoon_appknow_member_profile AS b ON b.userid = a.uid')
            ->where(array('a.id' => $askid))
            ->field('a.id,a.uid,a.content,a.addtime,a.thumb0,a.thumb1,a.thumb2,a.thumb3,a.thumb4,a.thumb5,a.catid,a.score,a.answer_number,b.truename,b.nickname,b.avatar,b.areaid,b.is_ok,b.memberships')
            ->order('addtime desc')
            ->select();
        foreach ($data AS $k => $v) {
            $data[$k]['addtime'] = date('Y-m-d', $v['addtime']);
            $data[$k]['address'] = implode(' ', $area->get_area_full_name_from_area_id($v['areaid']));
            for ($i = 0; $i < 6; $i++) {
                if ($v['thumb' . $i]) {
                    $data[$k]['ask_images'][] = $v['thumb' . $i];
                    $data[$k]['image_count'] = $i + 1;
                }
            }
        }
        return $data[0];
    }

    /**
     * 通过问题id获取提问用户真实姓名
     * @param $askid
     * @return mixed
     */
    public function get_ask_by_uid($askid)
    {
        $map['id'] = $askid;
        $uid = $this->where($map)->field('uid')->find();
        $member_profile = new MemberProfileModel();
        $truename = $member_profile->get_uid_by_name($uid['uid']);
        return $truename;
    }

    /**
     * 通过用户ID获取回答列表
     * @param $uid
     * @return array
     */
    public function get_uid_by_answer_list($uid)
    {
        $data = D('QuestionAnswer')
            ->field('askid')
            ->where(array('uid' => $uid))
            ->order('addtime desc')
            ->select();
        foreach ($data as $k => $v) {
            $askid[] = $v['askid'];
        }
        $askid = array_unique($askid);
        foreach ($askid as $k => $v) {
            $data = $this->get_id_by_ask_list($v);
            if (!empty($data)) {
                $list[] = $data;
            }
        }
        return $list;
    }

    /**
     * 获取当前问题悬赏金币数量
     * @param $askid [问题id]
     * @return mixed
     */
    public function gold_num($askid)
    {
        $map['id'] = $askid;
        $gold = $this->where($map)->field('gold')->find();
        return $gold;
    }

    /**
     * 获取提问列表具体执行方法
     * @param array $data
     * @return mixed
     */
    public function get_ask_list($data = array())
    {
        $arr_keyword = array();
        $map = "1=1";
        if ($data['cat_id'] != null) {
            $map .= " And catid = {$data['cat_id']}";
        }
        if ($data['keyword'] != null) {
            //分词搜索
            $arr_keyword = explode(',', Tools::scws($data['keyword']));
            $str = " AND ";
            foreach ($arr_keyword AS $k => $v) {
                $str .= " content like '%{$arr_keyword[$k]}%' OR";
            }
            $map .= substr($str, 0, -2);
        }
        if ($data['start'] != null && $data['limit'] != null) {
            $limit = $data['start'] . $data['limit'];
        }
        $request = $this
            ->alias('ask')
            ->join('LEFT JOIN __MEMBER_PROFILE__ profile ON ask.uid = profile.userid')
            ->join('LEFT JOIN destoon_ucenter_member m_member ON ask.uid = m_member.userid')
            ->where($map)
            ->field('ask.id,ask.uid,ask.content,ask.addtime,ask.thumb0,ask.thumb1,ask.thumb2,ask.thumb3,ask.thumb4,ask.thumb5,ask.areaid,ask.catid,ask.answer_number,ask.gold,profile.truename,profile.areaid,profile.address,profile.avatar,profile.memberships')
            ->order('addtime desc')
            ->limit($limit)
            ->select();
        foreach ($request as $k => $v) {
            $result = D('MemberProfile')->get_user_details_ask_list($v['uid']);
            $request[$k]['grade'] = $result['grade'];
            $request[$k]['answer_count'] = D('QuestionAnswer')->get_answer_number($v['id']);
            $request[$k]['memberships'] = $result['profile'][0]['member_profile']['memberships'];
            $request[$k]['medal'] = $result['medal'];
            //内容关键词匹配
            $request[$k]['keyword'] = implode(',', $arr_keyword);
        }
        return $request;
    }
}
